// created by noellynch
// May 6, 2009

package com.sceneworker.editors.rotationeditor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Callable;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

import com.jme.math.Quaternion;
import com.jme.scene.Spatial;
import com.jme.util.GameTaskQueueManager;
import com.myjavautils.swing.MigLayoutUtils;
import com.myjavautils.swing.SwingComponentFactory;

public class RotatorPanel extends JPanel {

	private		Spatial						mcl_spatial;
	private		EulerQuaternionEditorPanel	mcl_quatRep;
	private		JDialog						mcl_par;
	
	public	RotatorPanel(Spatial cl_spatial, JDialog cl_par) {
		mcl_spatial = cl_spatial;
		mcl_par = cl_par;
		
		MigLayoutUtils.asMigLayout(this);
		
		mcl_quatRep = new EulerQuaternionEditorPanel(cl_spatial.getLocalRotation(), true, null);
		add(mcl_quatRep);
		mcl_quatRep.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Rotation"),
                BorderFactory.createEmptyBorder(5,5,5,5)));
		
		JButton		lcl_apply = SwingComponentFactory.makeAButton("Apply");
		lcl_apply.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				final	Quaternion		lcl_rot = new Quaternion(mcl_quatRep.getQuaternion());
				
				GameTaskQueueManager.getManager().update(new Callable<Void>() {

					public Void call() throws Exception {
						mcl_spatial.setLocalRotation(lcl_rot);
						mcl_spatial.updateGeometricState(0, true);
						return null;
					}
					
				});
				
			}
		});
		
		
		JButton		lcl_done = SwingComponentFactory.makeAButton("Done");
		lcl_done.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mcl_par.setVisible(false);
			}
		});
		
		MigLayoutUtils.makeRightAlignedButtonBar(this, lcl_apply, lcl_done);
	}
}
